<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<style>
			#permissionEditForm .form-item{margin-bottom: 20px;}
			#permissionEditForm .form-item>label{min-width: 72px; display: inline-block; text-align: right;}
		</style>
		<div class="super-theme-example">
			<div class="options-toolbar">
				<div class="top-options">
					<a href="javascript:void(0)" class="easyui-linkbutton info" data-options="iconCls:'fa fa-plus'" onclick="permission.methods.addPermission(0)">新建</a>				
				</div>
			</div>
			<table id="permission-datagrid"></table>
			
			<div id="permissionEditWin" class="easyui-window" title="权限资源" style="width:500px;" data-options="modal:true,closed:true,minimizable:false,maximizable:false">
				<div class="super-theme-example">
					<form id="permissionEditForm">
						<input type="hidden" name="parentId">
						<input type="hidden" name="id">
						<input type="hidden" name="type">
						<div class="form-item">
			                <label for="" class="label-top">名称：</label>
			                <input name="name" class="easyui-textbox" style="width: 350px;" prompt="名称...">
			            </div>
			            <div class="form-item">
			                <label for="" class="label-top">排序：</label>
			                <input name="orderNum" class="easyui-textbox" style="width: 350px;" prompt="排序...">
						</div>
						<div class="form-item">
							<label for="" class="label-top">链接：</label>
							<input name="url" class="easyui-textbox" style="width: 350px;" prompt="链接...">
						</div>
						<div class="form-item">
							<label for="" class="label-top">值：</label>
							<input name="value" class="easyui-textbox" style="width: 350px;" prompt="值...">
						</div>
					</form>
					<div style="text-align: right; box-sizing: border-box;padding: 10px;">
						<a href="#" class="easyui-linkbutton" style="width: 80px;" onclick="$('#permissionEditWin').window('close');">取消</a>
						<a href="#" class="easyui-linkbutton primary" style="width: 80px;" onclick="permission.methods.editSubimt();">保存</a>
					</div>
				</div>
			</div>
		</div>
		
		<script>
			var permission = {
				datas: {
					expandIds: []
				},
				methods: {
					initDatagrid: function(){
						$('#permission-datagrid').treegrid({
							height: $(document).height() - 200,
						    url: commonConfig.mgUrlPrefix() + '/uaa/authority',
						    pagination: false,
						    idField: 'id',
						    treeField: 'name',
						    columns:[[
								{field:'name',title:'名称',width:100},
								{field:'type',title:'类型',width:100,align:'center',formatter: function(value,row,index){
									return value == 1 ? '目录' : value == 2 ? '菜单' : value == 3 ? '按钮' : '其他';
								}},
								{field:'value',title:'值',width:100,align:'center'},
								{field:'options',title:'操作',width:150,align:'center', formatter: function(value,row,index){
									return '<div class="row-edit-group">' +
										        ((row.type == 1 || row.type == 2) ? '<a onclick="permission.methods.addPermission('+row.id+')">新增</a>' : '') + 
										        '<a onclick="permission.methods.editPermission('+row.id+')">编辑</a>' +
										        '<a onclick="permission.methods.deletePermission('+row.id+')">删除</a>' +
										   '</div>';
								}}
						    ]],
						    onLoadSuccess: function(data){
						    	$('#permission-datagrid').treegrid('collapseAll');
						    	if(permission.datas.expandIds){
						    		$.each(permission.datas.expandIds, function(index , expandId) {
						    			$('#permission-datagrid').treegrid('expand' , expandId);
						    		});
						    	}
						    },
						    onExpand: function(row){
						    	if(row.type == 1){
						    		permission.datas.expandIds = [] ; 
						    	}
						    	permission.datas.expandIds.push(row.id); 
						    }
						});
					},
					addPermission: function(parentId){
						$('#permissionEditForm').form('clear');
						var _parent = permission.methods.findPermission(parentId) || {id: 0, type: 0};
						$('#permissionEditForm').form('load' , {parentId: _parent.id , type: _parent.type + 1});
						$('#permissionEditForm div.form-item').show();
						if(_parent.type == 0){
							$('#permissionEditForm div.form-item').eq(3).hide();
							$('#permissionEditForm div.form-item').eq(2).hide();
						}else if(_parent.type == 1){
							$('#permissionEditForm div.form-item').eq(3).hide();
						}else if(_parent.type == 2){
							$('#permissionEditForm div.form-item').eq(2).hide();
						}
						$('#permissionEditWin').window('open');
					},
					editPermission: function(permissionId){
						$('#permissionEditForm').form('clear');
						var _parent = permission.methods.findPermission(permissionId);
						if(!_parent){
							showMessage('<i class="fa fa-info-circle"></i>&nbsp; 未找到修改项...');
							return;
						}
						$('#permissionEditForm').form('load' , _parent);
						$('#permissionEditForm div.form-item').show();
						if(_parent.type == 1){
							$('#permissionEditForm div.form-item').eq(3).hide();
							$('#permissionEditForm div.form-item').eq(2).hide();
						}else if(_parent.type == 2){
							$('#permissionEditForm div.form-item').eq(3).hide();
						}else if(_parent.type == 3){
							$('#permissionEditForm div.form-item').eq(2).hide();
						}
						$('#permissionEditWin').window('open');
					},
					editSubimt: function(){
						var _params = $('#permissionEditForm').serializeJson() ; 
						if(_params.orderNum.trim() == '')_params.orderNum = 0 ; 
						if(_params.name.trim() == ''){
							$.messager.alert('提示', '名称不能为空','error');
							return false
						}
						if(_params.type.trim() == ''){
							$.messager.alert('提示', '权限类型不能为空','error');
							return false
						}
						requestBodyServer(commonConfig.mgUrlPrefix() + '/uaa/authority' , 'POST', _params).then((response) => {
							if (response.code == 1) {
								showMessage('<i class="fa fa-info-circle"></i>&nbsp; 请求成功...');
								$('#permissionEditWin').window('close');
								$('#permission-datagrid').treegrid('reload');
							}else {
								$.messager.alert('提示',response.message,'error');
							}
						}).catch((error) => {
							console.log(error)
						});
					},
					deletePermission: function(permissionId){
						$.messager.confirm('提示', '确定取消?', function(r){
							if(r){
								requestServer(commonConfig.mgUrlPrefix() + '/uaa/authority/'+permissionId , 'DELETE').then((response) => {
									if (response.code == 1) {
										showMessage('<i class="fa fa-info-circle"></i>&nbsp; 请求成功...');
										$('#permission-datagrid').treegrid('reload');
									}else {
										$.messager.alert('提示',response.message,'error');
									}
								}).catch((error) => {
									console.log(error)
								});
							}
						});
					},
					findPermission: function(id){
						var rowData = null;
						var _datas = $('#permission-datagrid').treegrid('getData');
						$.each(_datas , function(index , folder){
	    					if(folder.id == id){
	    						rowData = folder ; 
	    						return false;
	    					}
	    					if(folder.children && folder.children.length > 0){
	    						$.each(folder.children , function(index , _menu){
			    					if(_menu.id == id){
			    						rowData = _menu ; 
			    						return false;
			    					}
			    					if(_menu.children && _menu.children.length > 0){
			    						$.each(_menu.children , function(index , _button){
					    					if(_button.id == id){
					    						rowData = _button ; 
					    						return false;
					    					}
										});
			    					}
								});
	    					}
						});
						return rowData ; 
					}
				},
				init: function(){
					permission.methods.initDatagrid();
				}
			}
			$(function () {
				permission.init()
			})
		</script>
	</body>
</html>
